<?php

/**
 * This is the model class for table "tbl_notes".
 *
 * The followings are the available columns in table 'tbl_notes':
 * @property integer $id
 * @property string $dispatch
 * @property string $dateofnote
 * @property string $note
 * @property string $empno
 * @property string $custno
 * @property string $name
 * @property integer $private
 * @property integer $time
 */
class Notes extends CActiveRecord
{
    /**
     * Returns the static model of the specified AR class.
     * @return Notes the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'tbl_notes';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('note', 'required'),
            array('id, private', 'numerical', 'integerOnly' => true),
            array('dispatch', 'length', 'max' => 15),
            array('empno, custno', 'length', 'max' => 7),
            array('dateofnote,name', 'safe'),
            
		
			  
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, dispatch, dateofnote, note, empno, custno, private,time', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'dispatch' => 'Dispatch',
            'dateofnote' => 'Dateofnote',
            'note' => 'Note',
            'empno' => 'Empno',
            'custno' => 'Custno',
            'private' => 'Private',
        );
    }

    public static function getLastTechNotes($dispatch, $empno)
    {
        $notes = Yii::app()->db->createCommand('select note from tbl_notes where dispatch=:dispatch AND empno=:empno order by dateofnote limit 1')->bindValues(array(
            'empno' => $empno,
            'dispatch' => $dispatch
        ))->queryScalar();
        if ($notes !== false) {
            return $notes;
        }
        return '';
    }

    public static function getLastCustomerNotes($dispatch, $custno)
    {
        $notes = Yii::app()->db->createCommand('select note from tbl_notes where dispatch=:dispatch AND custno=:custno order by dateofnote desc limit 1')->bindValues(array(
            'custno' => $custno,
            'dispatch' => $dispatch
        ))->queryScalar();
        if ($notes !== false) {
            return $notes;
        }
        return '';
    }
    public function beforeFind() {
        parent::beforeFind();
    }

    public function beforeValidate(){
        if($this->isNewRecord){
            if($this->time == null){
                $this->time = date('H:i:s');
            }
        }
        return parent::beforeValidate();
    }
    public function beforeDelete() {
        $user=Users::model()->findByPk(Yii::app()->user->id);
        if($this->empno!=NULL && $user->employeeid==NULL){
            return false;
        }else if($this->custno!=NULL && $user->custno==NULL){
            return false;
        }
        return parent::beforeDelete();
    }
    public function beforeSave() {

        $this->name = Users::getUserNameById(Yii::app()->user->id);
        return  parent::beforeSave();
    }

    public function getAccessText(){
        if($this->private == 1 ){
            return 'Private';
        }else{
            return 'Public';
        }
    }

}